﻿using System;
using Alserve.Ffl.Data;

public partial class ffl_anonymous_webusercontrols_login : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.Cookies["Email"] != null)
            {
                EmailAddressTextBox.Text = Request.Cookies["Email"].Value;
                PasswordTextBox.Focus();
            }
            else
            {
                EmailAddressTextBox.Focus();
            }
        }
    }

    protected void LoginButton_OnClick(object sender, EventArgs e)
    {
        User thisUser = new User();
        Skip thisSkip = new Skip();
        
        thisUser.Where.Email.Value = EmailAddressTextBox.Text.Trim();


        if (thisUser.Query.Load())
        {
            if (thisUser.Password == PasswordTextBox.Text)
            {
                Session["UserId"] = thisUser.Id;
                Session["Admin"] = thisUser.Admin.ToString();
                Response.Cookies["Email"].Value = thisUser.Email;
                Response.Cookies["Email"].Expires = DateTime.Now.AddMonths(1);

                thisSkip.Where.Keyword.Value = "Status";
                thisSkip.Where.SkipNumber.Value = 0;
                if (thisSkip.Query.Load())
                    Server.Transfer("~//ffl//user//pickmyteam.aspx");
                else
                    Server.Transfer("~//ffl//user//mysquad.aspx");
            }
            else
            {
                ErrorTextLabel.Text = "Login details are incorrect";
            }
        }
        else
        {
            ErrorTextLabel.Text = "Login details are incorrect";
        }
        
    }

}
